﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
 * The file which stores material params will look like this
 *  
 * TreadRubber
 * 273.15,1050,16e6,0.47,-1,1950,0.25
 * 373.15,1024,12e6,0.47,-1,2250,0.241
 * 
 * Polyester
 * 273.15,1415,3e6,0.37,-1,1000,0.24
 * 373.15,1385,2.5e6,0.37,-1,1050,0.23
 * 
 * Steel
 * 273.15,7907,201.5e9,0.3,-1,450,52
 * 373.15,7879,195.5e9,0.3,-1,485,50
 * 
 * Polyamid nylon 6-6(pa)
 * 273.15,1305,2.1e9,0.3,-1,1695,0.25
 * 373.15,1285,1.7e9,0.3,-1,1715,0.24
 * 
 * <Name>
 * <Material params in same order they appear in TGM file>
 * ...
 * 
 * As many material parameters as wanted can be included per material
 * Materials are terminated by a blank line or EOF
 */

namespace TGMTireBuilder {
    public class MaterialParams {
        public double temp = 273.15, // Reference temperature, Kelvin
            density, // kg/m^3
            ym, // Young's Modulus/Elasticity Modulus, Pa
            pr, // Poison's Ratio
            df, // Damping factor. TGM quickstart says this isn't implemented, and should be -1
            sh, // Specific heat, ?
            tc; // Thermal conductivity, ?

        public MaterialParams() {}

        public MaterialParams(string input) { Parse(input); }

        public string ToString() {
            return temp.ToString() + "," +
                density.ToString() + "," +
                ym.ToString() + "," +
                pr.ToString() + "," +
                df.ToString() + "," +
                sh.ToString() + "," +
                tc.ToString();
        }

        public void Parse( string input ) {
            string[] parts = input.Split( ',' );
            if ( parts.Length != 7 )
                throw new FormatException( "MaterialParams tried to parse something unfamiliar" );

            temp =      double.Parse( parts[0] );
            density =   double.Parse( parts[1] );
            ym =        double.Parse( parts[2] );
            pr =        double.Parse( parts[3] );
            df =        double.Parse( parts[4] );
            sh =        double.Parse( parts[5] );
            tc =        double.Parse( parts[6] );
        }
    } // class MaterialParams

    public class Material {
        public string name;
        public List<MaterialParams> properties;

        public Material() {
            properties = new List<MaterialParams>();
        }

        public void ReadSingleMaterial( StreamReader str ) {
            name = str.ReadLine().Trim();
            string line = str.ReadLine().Trim();

            while ( line.Length > 1 && !str.EndOfStream ) {
                properties.Add( new MaterialParams( line ) );
                line = str.ReadLine();
            }
        }

        public void WriteToTGMStream( string prefix, StreamWriter str ) {
            for ( int i = 0; i <= properties.Count; i++ ) {
                str.WriteLine( prefix + "=(" + properties[i].ToString() + ")" );
            }
        }
    } // class Material

    class MaterialsLib {
        public List<Material> materials;

        public MaterialsLib() {
            materials = new List<Material>();
        }

        public void LoadMaterials() {
            try {
                using ( StreamReader str = new StreamReader( "Materials.txt" ) ) {
                    while ( !str.EndOfStream ) {
                        materials.Add( new Material() );
                        materials.Last().ReadSingleMaterial( str );
                    }
                    str.Close();
                }
            } catch ( Exception e ) {
                return;
            }
        }
    } // class MaterialsLib

}
